package com.cxyzy.camera2demo;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;

import java.io.File;
import java.io.IOException;

import id.zelory.compressor.Compressor;

import static com.cxyzy.camera2demo.Constants.INTENT_PATH_SAVE_PIC;

public class ShowImageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏模式
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_show_image);

        ImageView iv = findViewById(R.id.iv);

        if (getIntent() != null) {
            //图片地址
            String imagePath = getIntent().getStringExtra(INTENT_PATH_SAVE_PIC);
            if (!TextUtils.isEmpty(imagePath)) {
                File imageFile = new File(imagePath);
                compressImageFile(imageFile);
                iv.setImageBitmap(BitmapFactory.decodeFile(imageFile.getAbsolutePath()));
            }
        }
    }

    private void compressImageFile(File imageFile) {
        try {
            File compressedImageFile = new Compressor(this)
                    .setDestinationDirectoryPath(imageFile.getParent())
                    .compressToFile(imageFile, imageFile.getName() + ".jpg");
            if (compressedImageFile != null && compressedImageFile.exists() && compressedImageFile.length() > 0) {
                compressedImageFile.renameTo(imageFile);
            }
        } catch (IOException e) {
            LogUtils.e(e);
        }
    }
}
